﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DreamOnGameEngine.Main
{
    /// <summary>
    /// Manages processes
    /// </summary>
    public class CProcessManager
    {
        protected List<CProcess> m_ProcessList;
        /// <summary>
        /// Default Constructor
        /// </summary>
        public CProcessManager()
        {
            m_ProcessList=new List<CProcess>();
        }
        /// <summary>
        /// Attaches a process to the manager
        /// </summary>
        /// <param name="pProcess">Process to attach</param>
        public void Attach(CProcess pProcess)
        {
            m_ProcessList.Add(pProcess);
            pProcess.Attached = true;
        }
        /// <summary>
        /// Detatches a process
        /// </summary>
        /// <param name="pProcess">Process to detatch</param>
        private void Detatch(CProcess pProcess)
        {
            m_ProcessList.Remove(pProcess);
            pProcess.Attached = false;
        }
        /// <summary>
        /// is there any active proccesses of this type?
        /// </summary>
        /// <param name="nType">Type of process to check</param>
        /// <returns><c>true</c> if there is an active process of this type</returns>
        public bool IsProcessActive(PROCESS_TYPE nType)
        {
            foreach (CProcess i in m_ProcessList)
                if (i.PType == nType &&(i.Dead==false || i.Next!=null) )
                    return true;
            return false;
        }
        /// <summary>
        /// Does this manager have any processes?
        /// </summary>
        public bool HasProcessess
        {
            get { return m_ProcessList.Count != 0; }
        }
        /// <summary>
        /// Kills any processes still active
        /// </summary>
        ~CProcessManager()
        {
            List<CProcess> proc=m_ProcessList;
            foreach(CProcess i in proc)
            {
                Detatch(i);
            }
        }
        /// <summary>
        /// Updates all processes, kills any dead, and moves onto new ones
        /// </summary>
        /// <param name="deltaMilliseconds">milliseconds since last update</param>
        public void UpdateProcessess(int deltaMilliseconds)
        {
            int count = m_ProcessList.Count;

            for(int i=0;i<count;i++)
            {
                if (m_ProcessList[i].Dead)
                {
                    CProcess pNext = m_ProcessList[i].Next;
                    if (pNext != null)
                    {
                        m_ProcessList[i].Next = null;
                        Attach(pNext);
                    }
                    Detatch(m_ProcessList[i]);
                    i--;
                    count--;
                }
                else if (m_ProcessList[i].Active && !m_ProcessList[i].Paused)
                {
                    m_ProcessList[i].VOnUpdate(deltaMilliseconds);
                }
            }
        }
    }
}
